package Controler.Stage;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Controler.Fonctions;
import JDBC.Cours_Afficher;
import JDBC.Cours_Modifier;
import JDBC.Cours_SelectionLieu;
import JDBC.Professeur_Afficher;
import JDBC.Stage_Afficher;
import JDBC.Stage_Modifier;
import Model.Cours;
import Model.Lieu;
import Model.Listes;
import Model.Professeur;
import Model.Stage;

/**
 * Servlet implementation class Stage_ModifierController
 */
@WebServlet("/Stage_ModifierController")
public class Stage_ModifierController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Stage_ModifierController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String url = "Vues/VuesGrp3/VuesGrp3_Erreur/erreurModificationStage.jsp";
		HttpSession session = request.getSession();
		Listes listes = (Listes) session.getAttribute("listes");
		
		boolean dateValide=false;
		boolean heureValide=false;
		
		if(request.getParameter("out")!=null)
		{
			url = "Vues/VuesGrp3/vueGRP3_AfficheStage.jsp";
		}
		if(request.getParameter("out")==null)
		{
			int idModifie = Integer.parseInt(request.getParameter("idModif"));
			String date = request.getParameter("date");
			String heureDebut = request.getParameter("hd");
			String heureFin = request.getParameter("hf");

			
			if(Fonctions.verifierDate(date))
			{
				dateValide=true;
			}
			if(Fonctions.verifierHeure(heureDebut) && Fonctions.verifierHeure(heureFin))
			{
				if(Fonctions.comparerHeure(heureDebut,heureFin))
				{
					heureValide=true;
				}
			}
			
			if(dateValide && heureValide)
			{
				Stage s=new Stage();
				
				try
				{
					
					s.setIdStage(idModifie);
					s.setDateStage(date);
					s.setDuree(Integer.parseInt(request.getParameter("duree")));
					s.setHeureDebut(heureDebut);
					s.setHeureFin(heureFin);
					s.setId_Lieu(Integer.parseInt(request.getParameter("lieu")));	
					s.setId_Mat(Integer.parseInt(request.getParameter("matiere")));
					s.setId_Niv(Integer.parseInt(request.getParameter("niveau")));
					s.setId_Prof(Integer.parseInt(request.getParameter("professeur")));
					s.setCommentaire(request.getParameter("commentaire"));
					Stage_Modifier maj = new Stage_Modifier();
					maj.modifier(s);
					Professeur_Afficher listeProf = new Professeur_Afficher();
					ArrayList<Professeur> mesProfs = listeProf.afficherIntervenant();
					Stage_Afficher listeStage = new Stage_Afficher();
					ArrayList<Stage> mesStages = listeStage.afficherStage();
					listes.setMesStages(mesStages);
					listes.setMesIntervenants(mesProfs);
					session.setAttribute("listes", listes);
					url="Vues/VuesGrp3/vueGRP3_AfficheStage.jsp";
				}
				catch (Exception e)
				{
					url="Vues/VuesGrp3/VuesGrp3_Erreur/erreurModificationStage.jsp";
				}
			}
		}
		
		request.getRequestDispatcher(url).forward(request, response);
	}

	}


